spb/wp-spboucher-ai
Public
1import { createReadStream, promises as fs } from "node:fs";2import { Readable } from "node:stream";3import { join } from "node:path";4import { requireUser, jsonError } from "@/lib/auth/guard";5import { STORAGE_DIR } from "@/lib/env";6export const dynamic = "force-dynamic";7export async function GET(req: Request, { params }: { params: Promise<{ sha: string }> }) {8 try { await requireUser(); const { sha } = await params; if (!/^[0-9a-f]{64}$/.test(sha)) return new Response("Not found", { status: 404 }); const p = join(STORAGE_DIR, "previews", `${sha}.png`); await fs.access(p); return new Response(Readable.toWeb(createReadStream(p)) as unknown as ReadableStream, { headers: { "Content-Type": "image/png", "Cache-Control": "private, max-age=3600" } }); }9 catch (e) { if ((e as { code?: string }).code === "ENOENT") return new Response("Not found", { status: 404 }); return jsonError(e); }10}11